#include "ListThemeItem.h"
#include <aknutils.h>
#include "ListBox.h"

_LIT(KRQ,"\u4EBA\u6C14:");
_LIT(KSize,"\u5927\u5C0F:");
_LIT(KDownLoad,"\u4E0B\u8F7D:");
        
CListThemeItem* CListThemeItem::NewL(const CListBox* aOwner)
	{
	CListThemeItem* self = CListThemeItem::NewLC(aOwner);
    CleanupStack::Pop(self);
    return self;
	}

CListThemeItem* CListThemeItem::NewLC(const CListBox* aOwner)
	{
	CListThemeItem* self = new(ELeave) CListThemeItem();
    CleanupStack::PushL(self);
    self->ConstructL(aOwner);
    return self;
	}

CListThemeItem::CListThemeItem()
	{
	iControlType = EItem;
	iImage = NULL;
	iSize = NULL;
	iRQ = NULL;
	}

CListThemeItem::~CListThemeItem()
	{
	delete iSize;
	iSize = NULL;
	delete iRQ;
	iRQ = NULL;
	}

void CListThemeItem::ConstructL(const CListBox* aOwner)
	{
	iOwner = aOwner;
	iBmpTop = 10;
	ActivateL();
	}

TKeyResponse CListThemeItem::OfferKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType )
	{
	if (aType == EEventKey)
		{
		switch (aKeyEvent.iCode)
			{
			case EKeyEnter:
			case EKeyDevice3:
				return EKeyWasConsumed;
			}
		}
	return EKeyWasNotConsumed;
	}

void CListThemeItem::Draw(CWindowGc& gc, const TRect& aRect) const
	{
	//gc.SetClippingRect(aRect);
	CBaseControl::Draw(gc, aRect);
	TPoint position = Position();
	TPoint ptSecond;
	HBufC* disPlayText = NULL;
	TInt bitmapWidth = 0;
    if (iImage && iImage->Bitmap())
		{
		TRect bmpPieceRect(TPoint(0, 0), iImage->SizeInPixels());
		bitmapWidth = bmpPieceRect.Width();
		bitmapWidth += 10;
		if (iImage->MaskBitmap())
		    gc.BitBltMasked(TPoint(position.iX + iBmpLeft, position.iY + iBmpTop),iImage->Bitmap(),bmpPieceRect,iImage->MaskBitmap(),EFalse);
		else
		    gc.BitBlt(TPoint(position.iX + iBmpLeft, position.iY + iBmpTop), iImage->Bitmap(), bmpPieceRect);
		}
    //���
    if (iTitle)
    	{
    	gc.UseFont(iTitleFont);
    	gc.SetPenColor(iTitleColor);
    	ptSecond = TPoint(bitmapWidth + (Rect().iBr.iX - bitmapWidth) / 2 - 2, iTitlePosition.iY + iTitleFont->HeightInPixels() + 2);
    	TRect titleRect(iTitlePosition, ptSecond);
    	titleRect.Move(position);
    	TInt baseline = titleRect.Height() / 2 + iTitleFont->AscentInPixels() / 2;
    	gc.DrawText(iTitle->Mid(iCurOffset), titleRect, baseline, CGraphicsContext::ELeft, 0);
    	gc.DiscardFont();
    	}
    //��С
    if (iSize)
        {
        gc.UseFont(iTitleFont);
        gc.SetPenColor(iTitleColor);
        ptSecond.iY = iTitlePosition.iY;
        TRect titleRect(ptSecond, TPoint(Rect().iBr.iX - 2, iTitlePosition.iY + iTitleFont->HeightInPixels() + 2));
        titleRect.Move(position);
        TInt baseline = titleRect.Height() / 2 + iTitleFont->AscentInPixels() / 2;
        disPlayText = HBufC::NewL(KSize().Length() + iSize->Length());
        disPlayText->Des().Append(KSize());
        disPlayText->Des().Append(*iSize);
        gc.DrawText(*disPlayText, titleRect, baseline, CGraphicsContext::ELeft, 0);
        delete disPlayText;
        disPlayText = NULL;
        gc.DiscardFont();
        }
    //���ش���
    if (iText)
    	{
    	gc.UseFont(iTextFont);
    	gc.SetPenColor(iTextColor);
    	ptSecond.iY = iTextPosition.iY;
    	TRect textRect(ptSecond, TPoint(Rect().iBr.iX - 2, iTextPosition.iY + iTextFont->HeightInPixels() + 2));
    	textRect.Move(position);
    	TInt baseline = textRect.Height() / 2 + iTextFont->AscentInPixels() / 2;
        disPlayText = HBufC::NewL(KDownLoad().Length() + iText->Length());
        disPlayText->Des().Append(KDownLoad());
        disPlayText->Des().Append(*iText);
    	gc.DrawText(*disPlayText, textRect, baseline, iTextAlign, 0);
        delete disPlayText;
        disPlayText = NULL;
    	gc.DiscardFont();
    	}
    //����
    if (iRQ)
        {
        gc.UseFont(iTextFont);
        gc.SetPenColor(iTextColor);
        ptSecond.iY = iTextPosition.iY + iTextFont->HeightInPixels() + 2;
        TRect textRect(iTextPosition, ptSecond);
        textRect.Move(position);
        TInt baseline = textRect.Height() / 2 + iTextFont->AscentInPixels() / 2;
        disPlayText = HBufC::NewL(KRQ().Length() + iRQ->Length());
        disPlayText->Des().Append(KRQ());
        disPlayText->Des().Append(*iRQ);
        gc.DrawText(*disPlayText, textRect, baseline, iTextAlign, 0);
        delete disPlayText;
        disPlayText = NULL;
        gc.DiscardFont();
        }
    //gc.CancelClippingRect();
	}

void CListThemeItem::SetSizeL(const TDesC& aValue)
{
    delete iSize;
    iSize = NULL;
    iSize = aValue.AllocL();
}

void CListThemeItem::SetRQL(const TDesC& aValue)
{
    delete iRQ;
    iRQ = NULL;
    iRQ = aValue.AllocL();
}
